<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-suffix="：" label-width="90px">
      <el-form-item label="培训学校" prop="trainingSchoolId">
        <el-autocomplete v-model="queryParams.schoolName" :fetch-suggestions="querySearchAsync" placeholder="请输入培训学校名称" @select="handleSelect" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="工号" prop="sno">
        <el-input v-model="queryParams.sno" placeholder="请输入工号" clearable style="width: 180px;" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="姓名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入姓名" clearable style="width: 180px;" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="queryParams.mobile" placeholder="请输入手机号" clearable style="width: 180px;" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['train:staff:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="staffList" border>
      <el-table-column label="" align="center" prop="id" width="80"/>
      <el-table-column label="培训学校" align="center" prop="trainingSchoolName"/>
      <el-table-column label="教职工状态" align="center" prop="staffType"/>
      <el-table-column label="工号" align="center" prop="sno"/>
      <el-table-column label="姓名" align="center" prop="userName"/>
      <el-table-column label="手机号" align="center" prop="mobile"/>
      <el-table-column label="性别" align="center" prop="gender">
        <template slot-scope="{row}">
          <dict-tag :options="dict.type.sys_user_sex" :value="row.gender"/>
        </template>
      </el-table-column>
      <el-table-column label="是否绑定公众号" align="center" prop="follow"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDetail(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改培训学校员工对话框 -->
    <el-dialog title="员工信息" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-suffix="：">
        <el-form-item label="培训学校" prop="trainingSchoolId">
          {{form.trainingSchoolName}}
        </el-form-item>
        <el-form-item label="工号" prop="sno">
          {{form.sno}}
        </el-form-item>
        <el-form-item label="姓名" prop="userName">
          {{form.userName}}
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          {{form.mobile}}
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <dict-tag :options="dict.type.sys_user_sex" :value="form.gender"/>
        </el-form-item>
        <el-form-item label="是否绑定公众号" prop="follow">
          {{form.follow}}
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listStaff, getStaff} from "@/api/train/training/staff";
import {listTraining} from "@/api/train/training/school";

export default {
  name: "Staff",
  dicts: ['sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 培训学校员工表格数据
      staffList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        trainingSchoolId: null,
        staffType: null,
        sno: null,
        userName: null,
        mobile: null,
        gender: null,
        follow: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询培训学校员工列表 */
    getList() {
      this.loading = true;
      listStaff(this.queryParams).then(response => {
        this.staffList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        trainingSchoolId: null,
        staffType: null,
        sno: null,
        userName: null,
        mobile: null,
        gender: null,
        follow: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    querySearchAsync(queryString, cb) {
      this.queryParams.trainingSchoolId = undefined

      if (queryString) {
        listTraining({schoolName: queryString}).then(response => {
          let results = response.rows.map(item => {
            return {
              value: item.schoolName,
              id: item.id
            }
          });

          cb(results);
        });
      } else {
        cb([]);
      }
    },
    handleSelect(item) {
      this.queryParams.trainingSchoolId = item.id
    },
    /** 修改按钮操作 */
    handleDetail(row) {
      this.reset();
      const id = row.id
      getStaff(id).then(response => {
        this.form = response.data;
        this.open = true;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('train/staff/export', {
        ...this.queryParams
      }, `staff_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
